-- #################################################################################################
-- ##                                                                                             ##
-- ##                                   Auto Life Saver                                           ##
-- ##                                Version 2.1 final                                            ##
-- ##                                                                                             ##
-- ##                            Written by Wursti                                                ##
-- ##                                                                                             ##
-- #################################################################################################

-- #################################################################################################
-- ##                               Main Features & Changelog                                     ##
-- #################################################################################################
-- ## 2.0 - Never really worked as expected so version 2 should fix all :)                        ##
-- ## 2.1 - Added Particle Check (Mainly for HP Pots) :)                                          ##
-- #################################################################################################

-- #################################################################################################
-- ## TODO:                                                                                       ##
-- ## - Nothing ATM                                                                               ##
-- #################################################################################################

-- #################################################################################################
-- ## Please Send me your Feedback so I can improve this Script further :)                        ##
-- #################################################################################################


local player = GetMyHero()
local nextcheck = GetGameTimer()
local HealthCurrent = 0 
local HealthBefore = 0
local NextCheck = GetTickCount()
local HealthProc = 0
ItemBurstCheck = 0

function InitCheck()
for i=1, #itemCodes do
CheckPurchase()
end
end

function GetItemCodes()
itemCodes = {
				{name = "Zhonyas Hourglass", id= 3157, perc= 25, dropperc = 20, def = true, check = false, particle = nil},
				{name = "Wooglet's Witchcap", id= 3090, perc= 25, dropperc = 20, def = true, check = false, particle = nil},
				{name = "Seraph's Embrace", id= 3040, perc= 25, dropperc = 20, def = true, check = false, particle = nil},
				{name = "Health Potion", id= 2003, perc= 25, dropperc = 20, def = true, check = true, particle = "Regenerationpotion_itm.troy"},
				{name = "Elixir of Fortitude", id= 2037, perc= 25, dropperc = 20, def = true, check = false, particle = nil},
			}
UsableItems = {}
CheckParticles = {}
end

function OnLoad()
GetItemCodes()
InitCheck()
PrintChat(">> Auto Live Saver!")
PrintChat(">> Version 2.1 - One Pot at a Time")
end

function Menu(ItemToCheck,ItemName,TheID,pperc,droppperc,ddef,checkk,particlee)
l = ItemToCheck
_G["SConfig"..l] = scriptConfig(ItemName.." Saver","Auto"..l)
PrintChat(ItemName.." found!")
_G["SConfig"..l]:addParam("useItem", "Use Item: "..ItemName, SCRIPT_PARAM_ONOFF, ddef)
_G["SConfig"..l]:addParam("Burst", "Use it if drop set % hp in 1s", SCRIPT_PARAM_SLICE, droppperc, 1, 100, 0)
_G["SConfig"..l]:addParam("minHP", "Total min HP Percentage to use Item", SCRIPT_PARAM_SLICE, pperc, 1, 100, 0)
table.remove (itemCodes, TheID)
if checkk == true then
	table.insert (UsableItems, {ID = ItemToCheck, NAME = ItemName, MenuID = l, MustCheck = true})
	table.insert (CheckParticles, {ID = ItemToCheck, PARTICLE = particlee})
else
	table.insert (UsableItems, {ID = ItemToCheck, NAME = ItemName, MenuID = l, MustCheck = false})
end

end

function CheckPurchase()
for i, itemCode in pairs(itemCodes) do
	ThisItemName = itemCode.name
	ThisItemID = itemCode.id
	ThisID = i
	ThisPerc = itemCode.perc
	ThisDropPerc = itemCode.dropperc
	ThisDef = itemCode.def
	ThisCheck = itemCode.check
	ThisParticle = itemCode.particle
	if GetInventoryHaveItem(ThisItemID, player) == true then
		Menu(ThisItemID,ThisItemName,ThisID,ThisPerc,ThisDropPerc,ThisDef,ThisCheck,ThisParticle)
	end
end
end

function BBurst(percent,ItemSlot)
	HealthProc = (player.maxHealth*(percent/100))
	HealthCurrent = myHero.health
	if player:CanUseSpell(ItemSlot) then
		if HealthCurrent < (HealthBefore-HealthProc) then
			if player:CanUseSpell(ItemSlot) then
				PrintChat("Cast!")
				CastSpell(ItemSlot)
			end
		end
	end
	HealthBefore = myHero.health
end

function HPCheck(percent,ItemSlot)
	if player.health < (player.maxHealth*(percent/100)) then
		if player:CanUseSpell(ItemSlot) then
			CastSpell(ItemSlot)
		end
	end
end

function Check()
	for i, ItemReady in pairs(UsableItems) do
		CheckItemId = ItemReady.ID
		CheckItemName = ItemReady.NAME
		kay = ItemReady.MenuID
		Checker = ItemReady.MustCheck
		ActiveCheck = _G["Active"..CheckItemId]
		if GetInventoryHaveItem(CheckItemId, player) and (ActiveCheck == false or ActiveCheck == nil) then
			ThisScript = (_G["SConfig"..kay])
			if ThisScript.useItem then 
				HPperc = ThisScript.minHP
				Burstperc = ThisScript.Burst
				ItSlot = GetInventorySlotItem(CheckItemId)
				HPCheck(HPperc,ItSlot)
				ItemBurstCheck = _G["burst"..kay]
				if ItemBurstCheck == nil then ItemBurstCheck = 0 end
				if GetTickCount() >= ItemBurstCheck then
					_G["burst"..kay] = GetTickCount() + 1000
					BBurst(Burstperc,ItSlot)
				end
			end
		end
	end
end

function OnDeleteObj(object)
	for i, PartiChecker in pairs(CheckParticles) do
		CheckParticle = PartiChecker.PARTICLE
		ParticleItemID = PartiChecker.ID
		if object ~= nil and object.name == CheckParticle then
			_G["Active"..ParticleItemID] = false
		end
	end
end

function OnCreateObj(object)
	for i, PartiChecker in pairs(CheckParticles) do
		CheckParticle = PartiChecker.PARTICLE
		ParticleItemID = PartiChecker.ID
		if object ~= nil and object.name == CheckParticle then
			_G["Active"..ParticleItemID] = true
		end
	end
end

function OnTick()
if GetTickCount() >= nextcheck then
	nextcheck = GetTickCount() + 1000 
	CheckPurchase()
end
if #UsableItems ~= 0 then Check() end
end